<?php

namespace app\api\controller\classes;

use app\common\controller\Api;

//use app\common\controller\Backend;
use think\db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Student extends Api
{
    protected $noNeedRight = ['*'];
    /**
     * Student模型对象
     * @var \app\admin\model\classes\Student
     */
    protected $model = null;
    protected $model_user = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classes\Student;
        $this->model_user = model('user');

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        $class_id = $this->request->post('class_id/d', 0);
        if (empty($class_id)) {
            return $this->error('缺少重要参数');
        }
        $ClassessModel = new \app\admin\model\classes\Classes();
        $userinfo = $this->auth->getUserinfo();
        $profile_id = Db::name('classes_teacher')->where('user_id',$userinfo['user_id'])->value('profile_id');
        if (null == $Classess = $ClassessModel->find($class_id)) {
            return $this->error('班级不存在#1');
        }
        if ($Classess->profile_id!=$profile_id){
            return $this->error('班级不存在#2');
        }
        $where=[];
//            $param = $this->request->get();
//        if (!empty($param['class_id'])) {
            $uwhere['classs'] = $class_id;
            $sort = 'student_number';
            $order = 'asc';
//        }
        $limit=10;
        $list = $this->model_user
            ->where($where)
            ->where($uwhere)
            ->order($sort, $order)
            ->paginate($limit);

        foreach ($list as $row) {
            $class_list = Db::name('classes_class')->find($row->classs);
            $row->classs = $class_list['title'];
        }

        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);

    }

    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            Db::startTrans();
            try {
//                $params['school'] = get_admin_id($this->auth->id);
                $params['createtime'] = time();
                $params['status'] = 'normal';
                Db::name('user')->insert($params);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            return $this->success('添加成功');
        }
        $where = [];
//        if ($this->auth->id != 1) {
//            $where = ['uid' => get_admin_id($this->auth->id)];
//        }
//        $agalist = Db::name('auth_group_access')->where('uid', $this->auth->id)->find();
//        if ($agalist['group_id'] == 3) {
//            $tlist = Db::name('classes_teacher')->where(['user_id' => $this->auth->id])->find();
//            $where['profile_id'] = $tlist['profile_id'];
//        }
//
//        $clist = Db::name('classes_class')->where($where)->select();
//        $this->view->assign('clist', $clist);
//        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = Db::name('user')->find($ids);
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
//            dump($params);die;
            Db::startTrans();
            try {

                Db::name('user')->where('id', $row['id'])->update($params);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
           return  $this->success('成功');
        }else{
            return $this->error('请求方式出错');
        }

    }

    public function restore($ids = null)
    {
        $list = Db::name('user')->find($ids);
        if (!empty($list)) {
            Db::name('user')->where('id', $ids)->update(['verify_status' => 1]);
            $this->success();
        }
        $this->error('用户错误!');
    }
}
